package cn.doitedu.producer.low.datasources;

import java.util.Iterator;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

public class RandomDataSourceIter implements Iterator<String> {

    private int limit;
    private int count;
    private String template = "{\"user_id\":$_1,\"event_id\":\"$_2\",\"properties\":{\"f1\":$_3,\"f2\":$_4,\"f3\":$_5}}";

    public RandomDataSourceIter(String limit) {
        this.limit = Integer.parseInt(limit);
    }

    @Override
    public boolean hasNext() {
        return count < limit;
    }

    @Override
    public String next() {

        String userId = RandomStringUtils.randomNumeric(3);
        String eventId = RandomStringUtils.randomAlphabetic(4).toUpperCase();

        double f1 = RandomUtils.nextDouble();
        double f2 = RandomUtils.nextDouble();
        double f3 = RandomUtils.nextDouble();

        String tmp1 = template.replace("$_1", userId);
        String tmp2 = tmp1.replace("$_2", eventId);
        String tmp3 = tmp2.replace("$_3", f1 + "");
        String tmp4 = tmp3.replace("$_4", f2 + "");
        String res = tmp4.replace("$_5", f3 + "");

        count++;

        return res;
    }
}
